package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    @Override
    public List<AgreementItemVo> getAgreementItemVoList() {
        Long userId = LoginUserContext.getContext().getUserId();
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return leaseAgreementMapper.getAgreementItemVoList(userInfo.getPhone());
    }

    @Override
    public AgreementDetailVo getAgreementDetailVo(Long id) {
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        LeaseAgreement leaseAgreement = super.getById(id);
        if (leaseAgreement == null) {
            return null;
        }
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);

        Long apartmentId = leaseAgreement.getApartmentId();
        Long roomId = leaseAgreement.getRoomId();
        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.getApartmentDetailVo(apartmentId);
        RoomDetailVo roomDetailVo = roomInfoService.getDetailByRoomId(roomId);

        agreementDetailVo.setApartmentName(apartmentDetailVo.getName());
        agreementDetailVo.setApartmentGraphVoList(apartmentDetailVo.getGraphVoList());
        agreementDetailVo.setRoomNumber(roomDetailVo.getRoomNumber());
        agreementDetailVo.setRoomGraphVoList(roomDetailVo.getGraphVoList());

        Long paymentTypeId = leaseAgreement.getPaymentTypeId();
        Long leaseTermId = leaseAgreement.getLeaseTermId();
        PaymentType paymentType = paymentTypeService.getById(paymentTypeId);
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        LeaseTerm leaseTerm = leaseTermService.getById(leaseTermId);
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }
}




